﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Game
{
    class Program
    {
       
        enum Who { PLAYER, COM };

        static void Main(string[] args) {
            RunGame(ReadCommand(args));
        }

        private static string ReadCommand(string[] args) {
            for (int i = 0 ; i < args.Length - 1 ; ++i) {
                if (args[i] == "-remote") {
                    return args[i + 1];
                }
            }

            return "";
        }


        private static void RunGame(string remoteId ="") {
            bool remotePlay = false;
            SlimeGame MyGame = new SlimeGame();
            UI myUI = MyGame.GetUI();

            RemotePlayer remotePlayer = null;
            if (remoteId != "") {
                remotePlay = true;
                remotePlayer = new RemotePlayer(myUI, remoteId);
            }

            AI Player2 = new AI(myUI, (int)Who.COM);
            int Winner = 0; // 0: none, 1: Player 1, 2: Player 2
            List<Dictionary<string, int>> aiLog = null;
            while (Winner == 0) {
                bool TurnOver = false;
                
                while (!TurnOver) {
                    System.Console.Clear();
                    string input = "AI";
                    int turn = myUI.TurnToWho;
                    myUI.Display();
                    Console.Write("Turn to Player {0}\n", turn + 1);

                    if (turn == 0) {
                        if (remotePlay) {
                            if (aiLog != null) { remotePlayer.AILog = aiLog; }
                            TurnOver = remotePlayer.Run();
                        } else {
                            input = Console.ReadLine();
                            string[] inputs = input.Split(' ');
                            TurnOver = ExecuteOrders(myUI, turn, inputs);
                        }
                        
                    } else {
                        TurnOver = Player2.Run();                        
                    }
                    aiLog = Player2.AtkAction;
                }
                MyGame.Next();
                Winner = MyGame.Over();
            }//end while

            if (Winner == 1) {
                Console.WriteLine("Player 2 Win!");
                Console.Read();
            } else if (Winner == 2) {
                Console.WriteLine("Player 1 Win!");
                Console.Read();
            }
        }

        private static bool ExecuteOrders(UI MyUI, int turn, string[] inputs) {
            if (inputs[0] == "quit")
                return true;

            if (inputs[0] == "end") {
                MyUI.Rest();
                return true;
            }

            if (inputs[0] == "AI") {
                //MyUI.AI();
                //return true;
                Console.WriteLine("暫不開放此功能");
            }else 
            if (inputs[0] == "move" && inputs.Count() >= 4) {
                char slime = Convert.ToChar(inputs[1]);
                int pos_x = Convert.ToInt32(inputs[2]);
                int pos_y = Convert.ToInt32(inputs[3]);
                MyUI.Move(slime, new Position(pos_x, pos_y));
            }
            
            else if (inputs[0] == "atk" && inputs.Count() >= 3) {
                char a = Convert.ToChar(inputs[1]);
                char b = Convert.ToChar(inputs[2]);
                MyUI.Atk(a, b);
            }
            
            else if (inputs[0] == "state" && inputs.Count() >= 2) {
                char a = Convert.ToChar(inputs[1]);
                MyUI.State(a);
            }
            
            else if (inputs[0] == "born" && inputs.Count() >= 4) {
                char a = Convert.ToChar(inputs[1]);
                int b = Convert.ToInt32(inputs[2]);
                int c = Convert.ToInt32(inputs[3]);
                MyUI.Born(a, b, c);
            }
            
            else if (inputs[0] == "rest" && inputs.Count() >= 2) {
                if (inputs[1] == "all") {
                    MyUI.Rest();
                } else {
                    char a = Convert.ToChar(inputs[1]);
                    MyUI.Rest(a);
                }
            }
            else {
                Console.WriteLine("沒有這個指令");
            }

            return false;
        }

    }  // end class Program

}
